﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ImmediateSourceUpdate.cs" company="Marcin Jastrzebski">
//   Copyright (c) Marcin Jastrzebski (marcin@marcinj.net)
// </copyright>
// <summary>
//   The immediate source update workaround for missing functionality in WinRT.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace mPassRT.Controls
{
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;

    /// <summary>The immediate source update workaround for missing functionality in WinRT.</summary>
    /// <remarks>Credit for this goes to Marco Minerva at 
    /// http://marcominerva.wordpress.com/2013/03/14/how-to-immediately-update-the-source-of-a-textbox-in-winrt/]
    /// </remarks>
    public class ImmediateSourceUpdate : DependencyObject
    {
        #region Static Fields

        /// <summary>Dependency property that enables the workaround.</summary>
        public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.RegisterAttached(
            "IsEnabled", typeof(bool), typeof(ImmediateSourceUpdate), new PropertyMetadata(false, OnIsEnabledChanged));

        /// <summary>The source attached property.</summary>
        public static readonly DependencyProperty SourceProperty = DependencyProperty.RegisterAttached(
            "Source", typeof(string), typeof(ImmediateSourceUpdate), new PropertyMetadata(default(string)));

        #endregion

        #region Public Methods and Operators

        /// <summary>Gets the value of IsEnabled attached property for the specified object.</summary>
        /// <param name="obj">The object.</param>
        /// <returns>The value of the property.</returns>
        public static bool GetIsEnabled(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsEnabledProperty);
        }

        /// <summary>Gets the value of the Source attached property for the specified object.</summary>
        /// <param name="obj">The object.</param>
        /// <returns>The value of the property.</returns>
        public static string GetSource(DependencyObject obj)
        {
            return (string)obj.GetValue(SourceProperty);
        }

        /// <summary>Sets the IsEnabled attached property for the specified object.</summary>
        /// <param name="obj">The object.</param>
        /// <param name="value">The new value.</param>
        public static void SetIsEnabled(DependencyObject obj, bool value)
        {
            obj.SetValue(IsEnabledProperty, value);
        }

        /// <summary>Sets the Source attached property for the specified object.</summary>
        /// <param name="obj">The object.</param>
        /// <param name="value">The new value.</param>
        public static void SetSource(DependencyObject obj, string value)
        {
            obj.SetValue(SourceProperty, value);
        }

        #endregion

        #region Methods

        /// <summary>Called when the value of the IsEnabled property changes.</summary>
        /// <param name="d">The object whose IsEnabled property changed.</param>
        /// <param name="e">The event arguments.</param>
        private static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var txt = d as TextBox;
            if (txt != null)
            {
                if ((bool)e.NewValue)
                {
                    txt.TextChanged += OnTextChanged;
                }
                else
                {
                    txt.TextChanged -= OnTextChanged;
                }
            }
        }

        /// <summary>Called when the text changes in the text box.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The event arguments.</param>
        private static void OnTextChanged(object sender, TextChangedEventArgs e)
        {
            var txt = sender as TextBox;
            if (txt != null)
            {
                txt.SetValue(SourceProperty, txt.Text);
            }
        }

        #endregion
    }
}